package com.daify.threadlearn.chapter01.order05;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author daify
 * @date 2019-06-10
 */
public class CacheTwo<T,K> implements Computable<T,K> {

    private final Map<T,K> cache = new ConcurrentHashMap<T, K>();

    private final Computable<T,K> c;

    public CacheTwo(Computable<T, K> c) {
        this.c = c;
    }

    public K compute(T arg) throws Exception {
        K k = cache.get(arg);
        if (k == null) {
            k = c.compute(arg);
            cache.put(arg,k);
        }
        return k;
    }
}
